package com.cgsd.heyu.ui.info;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.cgsd.heyu.R;
import com.cgsd.heyu.common.AppMobclickAgent;
import com.cgsd.heyu.common.AvatarManger;
import com.cgsd.heyu.common.Constants;
import com.cgsd.heyu.common.UserModeConfig;
import com.cgsd.heyu.entity.DefaultAvatarEntity;
import com.cgsd.heyu.entity.EventMessage;
import com.cgsd.heyu.presenter.user.UserContract;
import com.cgsd.heyu.presenter.user.UserPresenter;
import com.cgsd.heyu.ui.BaseActivity;
import com.cgsd.heyu.ui.login.AvatarSelectActivity;
import com.cgsd.heyu.widgets.dialog.InputDialog;
import com.cgsd.heyu.widgets.dialog.SexDialog;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

/**
 * Description : 个人信息
 * @author : shaohua
 * 时间：2019/12/3 16
 * 邮箱：only_dream136@sina.com
 */
public class InformationActivity extends BaseActivity<UserContract.Presenter> implements View.OnClickListener,UserContract.UpdateInfoView  {
    /**
     * 修改昵称弹窗
     */
    private InputDialog inputDialog;

    /**
     * 修改性别弹窗
     */
    private SexDialog sexDialog;
    private TextView tvNickName;
    private ImageView ivAvatar;
    private TextView tvSex;
    private TextView tvPhoto;
    /**
     * 更新数据
     */
    private UserModeConfig updateUser;

    public static void actionStart(Activity activity){
        AppMobclickAgent.onEvent(AppMobclickAgent.JIBENZILIAOYE);
        Intent intent = new Intent(activity,InformationActivity.class);
        activity.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_information;
    }

    @Override
    protected void initView() {
        new UserPresenter(this);
        tvNickName = findViewById(R.id.tv_nickName);
        ivAvatar = findViewById(R.id.iv_avatar);
        tvSex = findViewById(R.id.tv_sex);
        tvPhoto = findViewById(R.id.tv_photo);

        findViewById(R.id.layout_avatar).setOnClickListener(this);
        findViewById(R.id.layout_nickname).setOnClickListener(this);
        findViewById(R.id.layout_sex).setOnClickListener(this);
    }

    @Override
    protected void initData() {
        setUserData();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.layout_avatar:
                AppMobclickAgent.onEvent(AppMobclickAgent.JIBEN_TOUXIANG);
                AvatarSelectActivity.actionStart(this,AvatarSelectActivity.UPDATE_AVATAR);
                break;
            case R.id.layout_nickname:
                AppMobclickAgent.onEvent(AppMobclickAgent.JIBEN_NICHENG);
                showInputDialog();
                break;
            case R.id.layout_sex:
                AppMobclickAgent.onEvent(AppMobclickAgent.JIBEN_XINGBIE);
                showSexDialog();
                break;
            default:
        }
    }

    @Override
    protected boolean isRegisterEventBus() {
        return true;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(EventMessage event) {
        if (event.getCode() == Constants.EventCode.USER_REFRESH) {
            setUserData();
        }
    }

    /**
     * 设置用户数据
     */
    private void setUserData(){
        AvatarManger.setAvatar(this,UserModeConfig.getInstance().headImgUrl,ivAvatar);
        tvNickName.setText(UserModeConfig.getInstance().nickName);
        tvPhoto.setText(UserModeConfig.getInstance().mobileNo);
        if (UserModeConfig.SEX_MEN.equals(UserModeConfig.getInstance().gender)){
            tvSex.setText(getString(R.string.men));
        }else if (UserModeConfig.SEX_WOMEN.equals(UserModeConfig.getInstance().gender)){
            tvSex.setText(getString(R.string.women));
        }else {
            tvSex.setText(getString(R.string.unknown));
        }
    }

    /**
     * 修改昵称弹窗
     */
    private void showInputDialog() {
        if (inputDialog == null) {
            inputDialog = new InputDialog(this);
            inputDialog.setTitle(getString(R.string.input_nickname));
            inputDialog.setHintText(getString(R.string.please_input_hint_nickname));
            inputDialog.setDialogListener(new InputDialog.DialogListener() {
                @Override
                public void confirm(String content) {
                    inputDialog.dismiss();
                    updateUser = new UserModeConfig();
                    updateUser.nickName = content;
                    mPresenter.updateInfo(updateUser);
                }

                @Override
                public void cancel() {
                    inputDialog.dismiss();
                }
            });
        }
        inputDialog.show();
    }

    /**
     * 修改性别弹窗
     */
    private void showSexDialog() {
        if (sexDialog == null) {
            sexDialog = new SexDialog(this);
            sexDialog.setTitle(getString(R.string.select_sex));
            sexDialog.setDialogListener(new SexDialog.DialogListener() {
                @Override
                public void confirm(String sex) {
                    sexDialog.dismiss();
                    updateUser = new UserModeConfig();
                    updateUser.gender = sex;
                    mPresenter.updateInfo(updateUser);
                }

                @Override
                public void cancel() {
                    sexDialog.dismiss();
                }
            });
        }
        sexDialog.show();
    }

    @Override
    public void updateAvatarSuccess() {

    }

    @Override
    public void updateNickNameSuccess() {
        if (updateUser == null){
            return;
        }
        UserModeConfig userModeConfig = UserModeConfig.getInstance();
        userModeConfig.nickName = updateUser.nickName;
        UserModeConfig.setUserModeConfig(userModeConfig);
        setUserData();
        EventBus.getDefault().post(new EventMessage<>(Constants.EventCode.USER_REFRESH));
    }

    @Override
    public void updateSexSuccess() {
        if (updateUser == null){
            return;
        }
        UserModeConfig userModeConfig = UserModeConfig.getInstance();
        userModeConfig.gender = updateUser.gender;
        UserModeConfig.setUserModeConfig(userModeConfig);
        setUserData();
        EventBus.getDefault().post(new EventMessage<>(Constants.EventCode.USER_REFRESH));
    }

    @Override
    public void userAvatarList(List<DefaultAvatarEntity> entities) {

    }
}
